package com.tianbing.adlottery.controller.hordeadmin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;

public class IsServantOnlineInterceptor extends HandlerInterceptorAdapter {
	static Logger logger = Logger.getLogger(IsServantOnlineInterceptor.class);

	private SkinUtils skinUtils;

	private String failPage;

	/**
	 * 1. check whether the login user has created a store 2. check whether the
	 * store has been put in black list 3. etc.
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response);
		if (!auth.isServantUser()) {
			ModelAndView modelAndView = new ModelAndView(failPage);
			throw new ModelAndViewDefiningException(modelAndView);
		}
		return true;

	}

	public void setFailPage(String viewPage) {
		this.failPage = viewPage;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

}
